<template>
    <div class="title">添加账号</div>
    <el-form
      :model="formData"
      :rules="rules"
      label-width="auto"
      style="max-width: 400px"
      ref="formRef"
    >
      <el-form-item
        label="账号"
        prop="account"
      >
        <el-input
          v-model="formData.account"
          placeholder="请输入账号"
          type="text"
        >
      
        </el-input>
      </el-form-item>
      <el-form-item
        label="密码"
        prop="password"
      >
        <el-input
          v-model="formData.password"
          placeholder="请输入密码"
          type="password"
        >
      
        </el-input>
      </el-form-item>
      <el-form-item
        label="用户组"
        prop="userGroup"
      >
        <el-select
          v-model="formData.userGroup"
          placeholder="请选择用户组"
        >
          <el-option
            v-for="group in userGroups"
            :key="group"
            :label="group"
            :value="group"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          @click="submitForm"
        >
          立即添加
        </el-button>
        <el-button
          @click="resetForm"
        >
          重置
        </el-button>
      </el-form-item>
    </el-form>
  </template>
  
  <script setup>
import { ref } from 'vue';
import { addAccount } from '../../api/account'; 
import { ElMessage } from 'element-plus';
const userGroups = ref([
  '超级管理员',
  '普通管理员'
]);
const formData = ref({
  account: '',
  password: '',
  userGroup: ''
});
const rules = ref({
  account: [
    { required: true, message: '请输入账号', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' }
  ],
  userGroup: [
    { required: true, message: '请选择用户组', trigger: 'change' }
  ]
});
const formRef = ref(null); // 声明 formRef
console.log(formRef.value);

const submitForm = async () => {
  if (formRef.value) {
    await formRef.value.validate(async (valid) => {
      if (valid) {
          const response = await addAccount(formData.value); 
          
            console.log('账号添加成功', response);
            ElMessage({
              message: '账号添加成功',
              type: 'success'
            })

            resetForm();
                   }  
  });
  }
};
const resetForm = () => {
  formData.value = {
    account: '',
    password: '',
    userGroup: ''
  };
};
</script>
  
  <style lang="scss" scoped>
  .title {
    width: 100%;
    height: 50px;
    font-size: 20px;
    color: #333;
    line-height: 50px;
    padding-left: 20px;
    text-align: left;
    border-bottom: 1px solid #dcdfe6;
  }
  .el-form-item {
    margin-bottom: 20px;
  }
 :deep(.el-form-item__label-wrap ) {
    width: 100px;
    padding-left: 50px;
  }
  :deep(.el-tooltip__trigger) {
    width: 285px;
    padding: 0 15px;
    margin-left: 15px;
  }
  </style>